home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / comms / internet / web-related / apache_1.0.5 / cgi-src / phf.c < prev    next >
Text File  |  1996-02-16  |  7KB  |  202 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define    LF    10
  5. #define HTML_BREAK    printf("<P>%c", LF);
  6. typedef struct {
  7.     char name[128];
  8.     char val[128];
  9. } entry;
  10.  
  11. typedef struct {
  12.     char qfield[256];
  13.     int  qlen;
  14.     char qname[256];
  15. } fields;
  16.  
  17. void getword(char *word, char *line, char stop);
  18. char x2c(char *what);
  19. void unescape_url(char *url);
  20. void plustospace(char *str);
  21. void send_fd(FILE *f, FILE *fd);
  22. void send_doc(int which);
  23.  
  24. static fields idxfields[] = { {"Qalias", 32, "Alias"},
  25.                              {"Qname", 256, "Name" },
  26.                              {"Qemail", 128, "E-mail Address"},
  27.                              {"Qnickname", 120, "Nickname"},
  28.                              {"Qoffice_phone", 60, "Office Phone Number"},
  29.                              {"Qcallsign", 16, "HAM Callsign"},
  30.                              {"Qproxy", 64, "Proxy"},
  31.                              {"Qhigh_school", 30, "High School"},
  32.                              {"Qslip", 256, "SLIP Address"},
  33.                              {NULL, 0, NULL}
  34.                            };
  35.  
  36. static fields othersearchfields[] = { {"Qcurriculum", 64, "Curriculum"},
  37.                              {"Qphone", 64, "Phone Number" },
  38.                              {"Qaddress", 128, "Address"},
  39.                              {"Qoffice_address", 128, "Office Address"},
  40.                              {"Qhome_address", 128, "Home Address"},
  41.                              {"Qpermanent_address", 128, "Permanent Address"},
  42.                              {"Qpermanent_phone", 60, "Permanent Phone"},
  43.                              {"Qdepartment", 64, "Department"},
  44.                              {"Qtitle", 64, "Title"},
  45.                              {"Qproject", 256, "Project"},
  46.                              {"Qother", 256, "Other"},
  47.                              {"Qbirthday", 24, "Birthday"},
  48.                              {"Qcolleges", 120, "Colleges Attended"},
  49.                              {"Qleft_uiuc", 24, "Date/Month Person left UIUC"},
  50.                              {NULL, 0, NULL},
  51.                            };
  52.  
  53. void send_doc(int which) {
  54.     int x;
  55.  
  56.     printf("<TITLE>Form for CSO PH query</TITLE>%c", LF);
  57.     printf("<H1>Form for CSO PH query</H1>%c", LF);
  58.     printf("This form will send a PH query to the specified ph server.%c", LF);
  59.     HTML_BREAK
  60.     printf("<HR>%c", LF);
  61.  
  62.     printf("<FORM ACTION=\"http://%s:%s%s\">%c", getenv("SERVER_NAME"),
  63.            getenv("SERVER_PORT"), getenv("SCRIPT_NAME"), LF);
  64.  
  65.     printf("PH Server:<INPUT TYPE=\"text\" NAME=\"Jserver\" VALUE=\"ns.uiuc.edu\" MAXLENGTH=\"256\">%c", LF);
  66.     HTML_BREAK
  67.  
  68.     printf("<H3>At least one of these fields must be specified:</H3><UL>%c",LF);
  69.     for(x=0; idxfields[x].qlen != 0; x++) 
  70.         printf("<LI><INPUT TYPE=\"text\" NAME=\"%s\" MAXLENGTH=\"%d\">%s%c"
  71.                ,idxfields[x].qfield, idxfields[x].qlen, idxfields[x].qname,LF);
  72.  
  73.     printf("</UL>%c", LF);
  74.  
  75.     if (!(which&0x10)) {
  76.         printf("<A HREF=\"%s?Jform=%d\"><H3>Show additional fields to narrow query</H3></A>%c", getenv("SCRIPT_NAME"), (which | 0x10), LF);
  77.         }
  78.     else {
  79.         printf("<H3>Additional fields to narrow query:</H3><UL>%c",LF);
  80.  
  81.         for(x=0; othersearchfields[x].qlen != 0; x++)
  82.             printf("<LI><INPUT TYPE=\"text\" NAME=\"%s\" MAXLENGTH=\"%d\">%s%c"
  83.                    ,othersearchfields[x].qfield, othersearchfields[x].qlen,
  84.                    othersearchfields[x].qname,LF);
  85.  
  86.         printf("</UL>%c", LF);
  87.  
  88.         printf("<A HREF=\"%s?Jform=%d\">Show fewer query fields</A>%c", getenv("SCRIPT_NAME"), (which & 0x01), LF);
  89.         }
  90.  
  91.     HTML_BREAK
  92.  
  93.     if (!(which & 0x01)) {
  94.         printf("<A HREF=\"%s?Jform=%d\"><H3>Return more than default fields</H3></A>%c", getenv("SCRIPT_NAME"), (which | 0x01), LF);
  95.         }
  96.     else {
  97.         printf("<H3>Fields to return:</H3><UL>%c", LF);
  98.  
  99.         for(x=0; idxfields[x].qlen != 0; x++) 
  100.             printf("<LI><INPUT TYPE=\"checkbox\" NAME=\"return\" VALUE=\"%s\">%s%c", &(idxfields[x].qfield[1]), idxfields[x].qname, LF);
  101.  
  102.         for(x=0; othersearchfields[x].qlen != 0; x++)
  103.             printf("<LI><INPUT TYPE=\"checkbox\" NAME=\"return\" VALUE=\"%s\">%s%c", &(othersearchfields[x].qfield[1]), othersearchfields[x].qname, LF);
  104.  
  105.         printf("</UL>%c", LF);
  106.  
  107.         printf("<A HREF=\"%s?Jform=%d\">Return default fields</A>%c", getenv("SCRIPT_NAME"), (which & 0x10), LF);
  108.         }
  109.  
  110.     HTML_BREAK
  111.     printf("<INPUT TYPE=\"submit\">%c", LF);
  112.     printf("</FORM>%c", LF);
  113.  
  114.     printf("<HR>%c<ADDRESS>", LF);
  115.     printf("Questions, comments to: <a href=\"http://www.ncsa.uiuc.edu/SDG/People/jbrowne/jbrowne.html\">Jim Browne</a>%c", LF);
  116.     printf("</ADDRESS>%c", LF);
  117.         
  118. }
  119.  
  120. main(int argc, char *argv[]) {
  121.     entry entries[64];
  122.     register int x,m=0;
  123.     char *cl;
  124.     char returnstr[1024], typestr[4098], commandstr[8192], serverstr[256];
  125.     int atleastonereturn = 0, atleastonequery = 0, which = 0;
  126.     FILE *phfp;
  127.  
  128.     printf("Content-type: text/html%c%c",LF,LF);
  129.  
  130.     strcpy(returnstr, "return ");
  131.     strcpy(typestr, " ");
  132.  
  133.     cl = getenv("QUERY_STRING");
  134.  
  135.     if((!cl) || (!cl[0])) {
  136.         send_doc(0);
  137.         exit(1);
  138.     }
  139.  
  140.     for(x=0;cl[0] != '\0';x++) {
  141.         m=x;
  142.         getword(entries[x].val,cl,'&');
  143.         plustospace(entries[x].val);
  144.         unescape_url(entries[x].val);
  145.         getword(entries[x].name,entries[x].val,'=');
  146.     }
  147.  
  148.     for(x=0; x <= m; x++) {
  149. /*      printf("%s = %s %c", entries[x].name, entries[x].val, LF); */
  150.  
  151.         if (!strcmp(entries[x].name, "return")) {
  152.             strcat(returnstr, entries[x].val);
  153.             strcat(returnstr, " ");
  154.             atleastonereturn = 1;
  155.             }
  156.         else if ((entries[x].name[0] == 'Q') && strlen(entries[x].val)) {
  157.             strcat(typestr, &(entries[x].name[1]));
  158.             strcat(typestr, "=");
  159.             strcat(typestr, entries[x].val);
  160.             strcat(typestr, " ");
  161.             atleastonequery = 1;
  162.             }
  163.         else if (!strcmp(entries[x].name, "Jserver")) 
  164.             strcpy(serverstr, entries[x].val);
  165.         else if (!strcmp(entries[x].name, "Jform")) 
  166.             if (sscanf(entries[x].val, "%d", &which)) {
  167.                 send_doc(which);
  168.                 exit(1);
  169.                 }
  170.             else exit(1);
  171.         }       
  172.  
  173.     printf("<H1>Query Results</H1>%c", LF);
  174.     HTML_BREAK
  175.  
  176.     if (!atleastonequery) printf("<B>You did not enter a query!</B>%c",LF);
  177.     else {
  178.         strcpy(commandstr, "/usr/local/bin/ph -m ");
  179.         if (strlen(serverstr)) {
  180.            strcat(commandstr, " -s ");
  181.            /* RM 2/22/94 oops */
  182.            escape_shell_cmd(serverstr);
  183.            strcat(commandstr, serverstr);
  184.            strcat(commandstr, " ");
  185.            }
  186.         escape_shell_cmd(typestr);
  187.         strcat(commandstr, typestr);
  188.         if (atleastonereturn) {
  189.            escape_shell_cmd(returnstr);
  190.            strcat(commandstr, returnstr);
  191.         }
  192.  
  193.         printf("%s%c", commandstr, LF);
  194.         printf("<PRE>%c", LF);
  195.  
  196.         phfp = popen(commandstr,"r");
  197.         send_fd(phfp, stdout);
  198.  
  199.         printf("</PRE>%c", LF);
  200.         }
  201. }
  202.